#include <linux/module.h>
#include <linux/init.h>
#include <linux/init_task.h>
#include<linux/list.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/kernel.h> 
//入口函数
static int __init my_print_init(void)
{
   struct task_struct *task,*p;
   struct list_head *pos;
    
   int count = 0;//计数器count
    printk("there are some infomation about processes\n");
    task = &init_task;//task设为双指针的头节点，让它指向0进程的PCB

    list_for_each(pos,&task->tasks){//从双链表的头开始遍历，
        p=list_entry(pos,struct task_struct,tasks);//找到结构体struct task_struct的tasks字段所在的结构体地址，即找到该进程的PCB
        count++;
        printk("第%d个进程信息如下：\n",count);
        printk("name: %s, pid: %d, state: %d, exit_state: %d, exit_code: %d, exit_signal: %d, parent_pid: %d, parent_name: %s,  utime: %d, stime: %d\n",
	p->comm,
	p->pid,
	p->__state,
	p->exit_state,
	p->exit_code,
	p->exit_signal,
	(p->parent)->pid,
	(p->parent)->comm,
	p->utime,
	p->stime);
               
    }	// list tasks

    printk("总共有%d个进程\n",count);
    return 0;
}
//出口函数
static void __exit my_print_exit(void)
{
    printk("Finished!\n");
}
module_init(my_print_init);
module_exit(my_print_exit);
MODULE_LICENSE("GPL");

